package com.fuhao.ack.comfirm;

import com.fuhao.utils.RabbitUtils;
import com.rabbitmq.client.*;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

/**
 * ack消息确认机制之comfirm模式的普通模式（即单条串行确定，同步的）
 * 普通的comfirm模式：每发送一条消息后，调用 waitForConfirms()方法，等待服务器端 confirm。实际上是一种串行 confirm 了。
 * @author zhang
 */
public class Product1 {
    public static void main(String[] args) throws IOException, TimeoutException, InterruptedException {
        Connection connection = RabbitUtils.getConnection();
        Channel channel = connection.createChannel();
        channel.queueDeclare("ack_comfirm_nomal_queue",true,false,false,null);
        //选择comfirm模式
        channel.confirmSelect();
        String msg = "ack comfirm nomal queue";
        channel.basicPublish("","ack_comfirm_nomal_queue",null,msg.getBytes());
        //同步等待是否发送成功
        if(!channel.waitForConfirms()){
            System.out.println("send msg fail");
        }else {
            System.out.println("send msg  success");
        }
        channel.close();
        connection.close();
    }
}

